<?php
abstract class ALib
{
	// ------------------------------------------------------------------------------------
	/**
	 * @static
	 * @return CDbConnection
	 */
	protected static $_db = NULL;
	public static function db()
	{
		return NULL === self::$_db ? self::$_db = Yii::app()->getDb() : self::$_db;
	}

	// -------------------------------------------------------------------------------------
	/**
	 * @param  $Criteria
	 * @return mixed
	 */
	public function find($Criteria)
	{
		if(is_array($Criteria))
		{
			$data = $Criteria;
			$Criteria = new CDbCriteria();
			$Criteria->addColumnCondition($data);
			unset($data);
		}
		elseif(is_string($Criteria))
		{
			$data = $Criteria;
			$Criteria = new CDbCriteria();
			$Criteria->addCondition($data);
			unset($data);
		}

		$Criteria->limit = 1;
		$result = $this->findAll($Criteria);
		if(!empty($result))
		{
			return array_shift($result);
		}
	}

	// -------------------------------------------------------------------------------------
	/**
	 * @param string $table
	 * @param CDbCriteria $Criteria
	 * @return CDbDataReader
	 */
	protected function _prepareSelect($table, CDbCriteria $Criteria = NULL)
	{
		if(NULL === $Criteria)
		{
			$Criteria = new CDbCritetia;
		}
		return self::db()->createCommand(self::_getSelectQuery($table, $Criteria))->query($Criteria->params);
	}

	// -------------------------------------------------------------------------------------
	/**
	 * @static
	 * @param string $table
	 * @param CDbCriteria $Criteria
	 * @return string
	 */
	protected static function _getSelectQuery($table, CDbCriteria $Criteria = NULL)
	{
		if(NULL === $Criteria)
		{
			$Criteria = new CDbCritetia;
		}

		$sql = 'SELECT ' . ($Criteria->distinct ? 'DISTINCT ' : '') . $Criteria->select . ' FROM `' . $table . '`';

		if(!empty($Criteria->alias))
		{
			$sql .= ' AS `' . $Criteria->alias . '`';
		}

		if(!empty($Criteria->join))
		{
			$sql .= ' ' . $Criteria->join;
		}

		if(!empty($Criteria->condition))
		{
			$sql .= ' WHERE ' . $Criteria->condition;
		}

		if(!empty($Criteria->group))
		{
			$sql .= ' GROUP BY ' . $Criteria->group;
		}

		if(!empty($Criteria->order))
		{
			$sql .= ' ORDER BY ' . $Criteria->order;
		}

		if(!empty($Criteria->having))
		{
			$sql .= ' HAVING ' . $Criteria->having;
		}

		if(-1 != $Criteria->offset)
		{
			if(-1 != $Criteria->limit)
			{
				$sql .= ' LIMIT ' . intval($Criteria->offset) . ', ' . intval($Criteria->limit);
			}
			else
			{
				$sql .= ' LIMIT ' . intval($Criteria->offset) . ', 2147483647';
			}
		}
		elseif(-1 != $Criteria->limit)
		{
			$sql .= ' LIMIT ' . intval($Criteria->limit);
		}

		return $sql;
	}

	// -------------------------------------------------------------------------------------
	/**
	 * @param null $Criteria
	 * @param null $totalRows
	 * @return
	 */
	public function findAll($Criteria = NULL, &$totalRows = NULL)
	{
		if(is_array($Criteria))
		{
			$data = $Criteria;
			$Criteria = new CDbCriteria();
			$Criteria->addColumnCondition($data);
			unset($data);
		}
		elseif(is_string($Criteria))
		{
			$data = $Criteria;
			$Criteria = new CDbCriteria();
			$Criteria->addCondition($data);
			unset($data);
		}

		if(NULL === $Criteria)
		{
			$Criteria = new CDbCriteria;
		}

		return $this->_doFindAll($Criteria, $totalRows);
	}

	// -------------------------------------------------------------------------------------

	abstract protected function _doFindAll(CDbCriteria $Criteria, &$totalRows = NULL);

	// -------------------------------------------------------------------------------------
}